package cn.redragon.soa.security;

import java.util.Collection;
import lombok.EqualsAndHashCode;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;

@EqualsAndHashCode(callSuper = true)
public class CommonAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = -3142367526845846969L;

    private final String userName;

    private final String token;

    /**
     * Creates a token with the supplied array of authorities.
     *
     * @param authorities the collection of <tt>GrantedAuthority</tt>s for the principal
     *                    represented by this authentication object.
     */
    public CommonAuthenticationToken(final String userName, final String token, final Collection<? extends String> authorities) {
        super(AuthorityUtils.createAuthorityList(authorities.toArray(new String[0])));
        this.userName = userName;
        this.token = token;
        setAuthenticated(true);

    }

    @Override
    public Object getCredentials() {
        return token;
    }

    @Override
    public Object getPrincipal() {
        return userName;
    }
}
